#pragma strict

var model:Transform;
var mat:Material;

private var mesh:Mesh;
private var tr:Transform;

function Start () {
	mesh = model.GetComponent.<MeshFilter>().mesh;
	if(!mat){
		enabled = false;
	}
	tr = transform;
}

function OnPostRender () {
	GL.PushMatrix();
	
	mat.SetPass(0);
	GL.wireframe = true;
	// GL.Color(Color.red);
	GL.Begin(GL.TRIANGLES);
	
	for(var i:int = 0;i<mesh.triangles.length;i+=3){
		var t0:int = mesh.triangles[i];
		var t1:int = mesh.triangles[i+1];
		var t2:int = mesh.triangles[i+2];
		//transform to world space.
		GL.Vertex(model.TransformPoint(mesh.vertices[t0]));
		GL.Vertex(model.TransformPoint(mesh.vertices[t1]));
		GL.Vertex(model.TransformPoint(mesh.vertices[t2]));
	}
	GL.End();
	GL.PopMatrix();
	
	GL.wireframe = false;
}